<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods, index) in goodsList" :key="index" @click="gotoGoodDetail(goods.goods_id)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: '',
					page: 1,
					pageSize: 10
				},
				goodsList: [],
				total: 0,
				flag: false
			};
		},
		onLoad(options) {
			const { query = '', cid = '' } = options
			this.queryObj.query = query
			this.queryObj.cid = cid
			this.getGoodsList()
		},
		methods: {
			async getGoodsList(cb) {
				this.flag = true
				const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				cb && cb()
				this.flag = false
				if(res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
			},
			gotoGoodDetail(goods_id) {
				uni.navigateTo({
					url: `/subpak/good_detail/good_detail?goods_id=${goods_id}`
				})
			}
		},
		onReachBottom() {
			if(this.queryObj.page & this.queryObj.pageSize >= this.total) {
				return uni.$showMsg('数据加载完了')
			}
			if(this.flag) return
			this.queryObj.page++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.queryObj = {
				query: '',
				cid: '',
				page: 1,
				pageSize: 10
			}
			this.flag = false
			this.total = 0
			this.goodsList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">

</style>
